//
//  WQ_LoginViewController.swift
//  xinlang
//
//  Created by 联创—王增辉 on 2019/3/25.
//  Copyright © 2019年 lcWorld. All rights reserved.
//

import UIKit

class WQ_LoginViewController: UIViewController {

    var webView:UIWebView?
    override func loadView() {
        webView = UIWebView(frame: UIScreen.main.bounds)
        let path = "https://api.weibo.com/oauth2/authorize?client_id=\(SinaAppKey)&redirect_uri=\(SinaRedirectUri)"
        guard let url     = URL.init(string:  path) else{
                return
        }
        webView?.loadRequest(URLRequest.init(url: url))
        webView?.scrollView.isScrollEnabled = false
        webView?.delegate = self
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        setupNavi()
    }
}
// MARK: - 添加navigation
extension WQ_LoginViewController
{
    func setupNavi() {
        let item = UIButton()
        item.setTitle("返回", for: .normal)
        item.setTitleColor(UIColor.gray, for: .normal)
        item.setImage(UIImage.init(named: "navigationbar_back_withtext"), for: .normal)
        item.addTarget(self, action: #selector(back), for: .touchUpInside)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: item)
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: .done, target: self, action: #selector(autoFill))
        navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.orange], for: .normal)
    }
}
extension WQ_LoginViewController
{
    //自动填充
    @objc func autoFill() {
        webView?.stringByEvaluatingJavaScript(from:" document.getElementById('userId').value = '18134247957'")
        webView?.stringByEvaluatingJavaScript(from:  "document.getElementById('passwd').value = 'wuqiong123'")
    }
    @objc func back() {
        dismiss(animated: true, completion: nil)
    }
}
//MARK: - 代理
extension WQ_LoginViewController:UIWebViewDelegate {
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
        
        let url = request.url?.absoluteString
        
        if url!.hasPrefix(SinaRedirectUri) == false {
            return true
        }
        
        let query = request.url?.query
        
        if query?.hasPrefix("code=") == false {
            back()
        }else{
            guard let code = query?.suffix((query?.count)! - "code=".count ) else{
                return false
            }

            WQ_NetworkManager<WQ_accessTokenModel>().GetTokenRequst(code: String(code)) { (result) in
                guard let data = result.data else{
                    return
                }
                WQ_accessTokenModel.accessTokenModel.access_token = data.access_token
                WQ_accessTokenModel.accessTokenModel.isRealName = data.isRealName
                WQ_accessTokenModel.accessTokenModel.uid = data.uid
                WQ_accessTokenModel.accessTokenModel.remind_in = data.remind_in
                WQ_accessTokenModel.accessTokenModel.expires_in = data.expires_in
                WQ_accessTokenModel.accessTokenModel.save()
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: loginChangeNotfication), object: nil)
                self.dismiss(animated: true, completion: nil)
            }
            
        }
        return false
    }
}
